var $ = getApp(), methods = require("../methods.js");
var app = $
var SocketTask = null;
var interval0;
var interval1;
var interval2;
Page(Object.assign({}, methods, {
	data: {
		st: 1,
		datalist: [],
		pagenum: 1,
		nodata:1,
		keyword:'',
	},
	socketsend:function(senddata){
		console.log(SocketTask)
		var that = this;
		if(!SocketTask || SocketTask.readyState!=1){
			var pre_url = $.pre_url
			var wssurl = pre_url.replace('https://', "wss://") + '/wss';
			SocketTask = wx.connectSocket({url:wssurl});
			SocketTask.onOpen(function(){
				console.log('socketopen');
				SocketTask.send({data:JSON.stringify({type:'init',token:that.data.token,data:{ aid:that.data.aid,uid:that.data.uid}})})
				if(senddata){
					SocketTask.send({data:JSON.stringify(senddata)})
				}
			});
			SocketTask.onMessage(function(res){
				console.log(res)
				try{
					var data = JSON.parse(res.data);
					if(data.type == 'tokefu'){
						console.log(data.data)
						that.receiveMessage(data.data)
					}
				}catch(e){}
			})
		}else{
			if(senddata)
			SocketTask.send({data:JSON.stringify(senddata)})
		}
	},
	onHide: function () {
		clearInterval(interval0);
		clearInterval(interval1);
		clearInterval(interval2);
		if(SocketTask && SocketTask.readyState==1){
			SocketTask.close();
		}
	},
	receiveMessage: function (message) {
		this.onShow();
	},
	onShow:function(){
		var that = this
		var keyword = that.data.keyword
		this.setData({ pagenum:1,datalist:[],$nomoreData:false})
		$.get($.pre_url + '/am.php?s=/kefu/index',{keyword:keyword},function(res){
			if(res.status==0){
				$.alert(res.msg);return;
			}
			that.setData(res);
			that.socketsend({type:'connect',token:res.token});
			interval1 = setInterval(function(){
				that.socketsend({type:'connect',token:res.token});
			},25000)
		})
	},
	onPullDownRefresh: function () {
		this.onShow();
		wx.stopPullDownRefresh()
	},
	changetab: function (e) {
		var st = e.currentTarget.dataset.st
		this.setData({ pagenum:1,st: st,datalist:[],$nomoreData:false})
		wx.pageScrollTo({
			scrollTop:0,duration:0
		})
		this.getDataList()
	},
	getDataList: function(){
		var that = this
		var pagenum = that.data.pagenum
		var keyword = that.data.keyword
		var st = that.data.st
		$.post($.pre_url + '/am.php?s=/kefu/index',{pagenum:pagenum,keyword:keyword},function(res){
			var data = res.data
			if(pagenum == 1){
				that.setData({ datalist: data });
				if(data.length == 0){
					that.setData({ nodata:1});
					$.showEmpty('暂无记录','/images/empty2.png');
				}else{
					that.setData({ nodata:0});
				}
			}else{
				if (data.length == 0){
					that.setData({$nomoreData:true});
				}else{
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
					that.setData({ datalist: newdata });
				}
			}
    });
	},
	onReachBottom: function () {
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData){
			this.setData({ pagenum: this.data.pagenum+1 })
			this.getDataList();
		}
	},
	showdel:function(e){
		return ;
		var that = this
		var mid = e.currentTarget.dataset.mid
		wx.showActionSheet({
			itemList: ['删除'],
			success (res) {
				$.post($.pre_url + '/am.php?s=/kefu/del',{mid:mid},function(d){
					that.onShow();
				})
			},
			fail (res) {
				
			}
		})
	},
	searchChange: function (e) {
		this.setData({
			keyword: e.detail.value
		});
	},
	searchConfirm: function (e) {
		var that = this;
		var keyword = e.detail.value;
		that.setData({ keyword: keyword });
		that.onShow();
	},
}))